package com.shelpe.services.sinbad.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.shelpe.services.sinbad.entity.jpa.TargetTag;
import com.shelpe.services.sinbad.model.RptCreative;
import com.shelpe.services.sinbad.model.RptCustomer;
import com.shelpe.services.sinbad.model.RptKeyword;

import lombok.Data;

@Data
@JsonInclude(Include.NON_DEFAULT)
public class EffectMetricDto {

	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
	private Date date;

	private long campaignID;
	private long adgroupID;
	private long keywordID;
	@JsonProperty("creative_id")
	private long creativeID;
	@JsonProperty("crowd_id")
	private String crowdID;

	private float cvr = -1;
	private float favCountRate = -1;
	private float cartCountRate = -1;
	private int payCount = -1;
	private float sale = -1;
	@JsonProperty("fav_count")
	private float favCount = -1.0f;

	private float avgpos = -1;// 宝贝排名

	private float cpc = -1;// 平均点击花费
	private float ctr = -1;// 点击率
	private int impressions = -1;// 展现量
	private float cpm = -1;// 千次点击
	private int click = -1;// 点击量
	private int cost = -1;// 总花费
	private float roi = -1;// 投入产出比

	@JsonProperty("direct_pay")
	private int directPay = -1;// 直接成交金额
	@JsonProperty("indirect_pay")
	private int indirectPay = -1;// 间接成交金额
	@JsonProperty("direct_pay_count")
	private int directPayCount = -1;// 直接成交
	@JsonProperty("indirect_pay_count")
	private int indirectPayCount = -1;// 间接成交
	@JsonProperty("pay_total_count")
	private int payTotalCount = -1;// 总成交笔数
	@JsonProperty("pay_total")
	private int payTotal = -1; // 总成交金额
	@JsonProperty("fav_shop_count")
	private int favShopCount = -1;// 店铺收藏
	@JsonProperty("fav_item_count")
	private int favItemCount = -1;// 宝贝收藏
	@JsonProperty("fav_total")
	private int favTotal = -1;// 总收藏数
	@JsonProperty("direct_cart_total")
	private int directCartTotal = -1;// 直接购物车数
	@JsonProperty("indirect_cart_total")
	private int indirectCartTotal = -1;// 间接购物车数
	@JsonProperty("cart_total")
	private int cartTotal = -1;// 购物车总数
	private float coverage = -1;// 点击转化率
	private int wirelessQscore = -1;// 无线质量得分
	private int qscore = -1;// 无线质量得分
	@JsonProperty("cart_cost")
	private float cartCost = -1; // 加购成本=总花费/总加购数
	@JsonProperty("fav_cart_rate")
	private float favCartRate = -1;// 收藏加购率
	@JsonProperty("fav_cart")
	private float fav_cart = -1;// 收藏加购数
	@JsonProperty("favCartThroughput")
	private float fav_cart_throughput = -1;// 收藏加购所需流量 点击量/收藏加购数
	@JsonProperty("fav_cost")
	private float favCost = -1;// 收藏成本=总花费/总收藏数
	@JsonProperty("pay_count_throughput")
	private float payCountThroughput = -1;// 成交所需流量 点击量/成交笔数

	@JsonProperty("pay_count")
	private int paySum;

	public void from(RptKeyword rpt) {
		if (rpt != null) {
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
			if (rpt.getCampaignId() != null && rpt.getCampaignId().longValue() > 0) {
				this.campaignID = rpt.getCampaignId();
			}
			if (rpt.getAdgroupId() != null && rpt.getAdgroupId().longValue() > 0) {
				this.adgroupID = rpt.getAdgroupId().longValue();
			}
			if (rpt.getKeywordId() != null && rpt.getKeywordId().longValue() > 0) {
				this.keywordID = rpt.getKeywordId();
			}

			this.favCount = rpt.getFavItemCount() + rpt.getFavShopCount();
			this.paySum = rpt.getPayCount();
		}
	}

	public void from(RptCustomer rpt) {
		if (rpt != null) {
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
			this.payCount = (rpt.getDirectPayCount() != null ? rpt.getDirectPayCount() : 0)
					+ (rpt.getIndirectPayCount() != null ? rpt.getIndirectPayCount() : 0);
			this.payTotalCount = this.payCount;
			this.sale = (rpt.getDirectPay() != null ? rpt.getDirectPay() : 0)
					+ (rpt.getIndirectPay() != null ? rpt.getIndirectPay() : 0);
			if (this.cost != 0) {
				this.roi = this.sale / this.cost;
			}
			this.favCount = rpt.getFavShopCount() + rpt.getFavItemCount();
			if (rpt.getClick() != null && rpt.getClick() > 0) {
				this.cvr = 100 * (float) this.payCount / rpt.getClick();
			}
		}
	}

	public void from(RptCreative rpt) {
		if (rpt != null) {
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
		}
	}

	public void creativeFrom(List<RptCreative> rpts) {
		if (rpts != null) {
			impressions = impressions + 1;
			cpm = cpm + 1;
			click = click + 1;
			cost = cost + 1;
			directPay = directPay + 1;
			indirectPay = indirectPay + 1;
			directPayCount = directPayCount + 1;
			indirectPayCount = indirectPayCount + 1;
			favShopCount = favShopCount + 1;
			favItemCount = favItemCount + 1;
			directCartTotal = directCartTotal + 1;
			indirectCartTotal = indirectCartTotal + 1;
			for (RptCreative rpt : rpts) {
				impressions = impressions + rpt.getImpressions();
				cpm = cpm + rpt.getCpm();
				click = click + rpt.getClick();
				cost = cost + rpt.getCost();
				directPay = directPay + rpt.getDirectPay();
				indirectPay = indirectPay + rpt.getIndirectPay();
				directPayCount = directPayCount + rpt.getDirectPayCount();
				indirectPayCount = indirectPayCount + rpt.getIndirectPayCount();
				favShopCount = favShopCount + rpt.getFavShopCount();
				favItemCount = favItemCount + rpt.getFavItemCount();
			}
			payTotal = directPay + indirectPay;
			payTotalCount = directPayCount + indirectPayCount;
			favTotal = favShopCount + favItemCount;

			if (cost == 0) {
				roi = 0.0f;
			} else {
				roi = (float) payTotal / (float) cost;
			}
			if (click == 0) {
				cpc = 0.0f;
				ctr = 0.0f;
				coverage = 0.0f;
			} else {
				cpc = (float) cost / (float) click;
				ctr = (float) click / (float) impressions;
				coverage = (float) payTotalCount / (float) click;
			}
		}
	}

	public void from(List<TargetTag> tags) {
		if (tags != null) {
			impressions = impressions + 1;
			cpm = cpm + 1;
			click = click + 1;
			cost = cost + 1;
			directPay = directPay + 1;
			indirectPay = indirectPay + 1;
			directPayCount = directPayCount + 1;
			indirectPayCount = indirectPayCount + 1;
			favShopCount = favShopCount + 1;
			favItemCount = favItemCount + 1;
			directCartTotal = directCartTotal + 1;
			indirectCartTotal = indirectCartTotal + 1;
			for (TargetTag tag : tags) {
				impressions = impressions + tag.getImpressions();
				cpm = cpm + tag.getCpm();
				click = click + tag.getClick();
				cost = cost + tag.getCost();
				directPay = directPay + tag.getDirectPay();
				indirectPay = indirectPay + tag.getIndirectPay();
				directPayCount = directPayCount + tag.getDirectPayCount();
				indirectPayCount = indirectPayCount + tag.getIndirectPayCount();
				favShopCount = favShopCount + tag.getFavShopCount();
				favItemCount = favItemCount + tag.getFavItemCount();
				directCartTotal = directCartTotal + tag.getCartTotal();
				indirectCartTotal = indirectCartTotal + tag.getIndirectCartTotal();
			}
			payTotal = directPay + indirectPay;
			payTotalCount = directPayCount + indirectPayCount;
			cartTotal = directCartTotal + indirectCartTotal;
			favTotal = favShopCount + favItemCount;

			if (cost == 0) {
				roi = 0.0f;
			} else {
				roi = (float) payTotal / (float) cost;
			}
			if (click == 0) {
				cpc = 0.0f;
				ctr = 0.0f;
				coverage = 0.0f;
			} else {
				cpc = (float) cost / (float) click;
				ctr = (float) click / (float) impressions;
				coverage = (float) payTotalCount / (float) click;
			}
		}
	}

	public void addSemiautoDimension(RptKeyword rpt) {
		/** 此时的数据都是经过过滤后的，故没有封装类型的数据 */
		float favCart = rpt.getCartTotal() + rpt.getFavCount(); // 收藏加购数
		if (rpt.getCartTotal() != 0) {
			this.setCartCost(rpt.getCost() / rpt.getCartTotal());
		}
		if (rpt.getClick() != 0) {
			this.setFavCartRate(favCart / rpt.getClick());
		}
		if (favCart != 0f) {
			this.setFav_cart_throughput(rpt.getClick() / favCart);
			this.setFavCost(rpt.getCost() / favCart);
		}
		if (rpt.getPayCount() != 0) {
			this.setPayCountThroughput(rpt.getClick() / rpt.getPayCount());
		}
		this.setFav_cart(favCart);
	}

	public void filterNull(RptKeyword rpt) {
		if (rpt.getCartTotal() == null) {
			rpt.setCartTotal(0);
		}
		if (rpt.getCpm() == null) {
			rpt.setCpm(0f);
		}
		if (rpt.getAvgpos() == null) {
			rpt.setAvgpos(0f);
		}
		if (rpt.getDirectCartTotal() == null) {
			rpt.setDirectCartTotal(0);
		}
		if (rpt.getDirectPay() == null) {
			rpt.setDirectPay(0);
		}
		if (rpt.getDirectPayCount() == null) {
			rpt.setDirectPayCount(0);
		}
		if (rpt.getFavItemCount() == null) {
			rpt.setFavItemCount(0);
		}
		if (rpt.getFavShopCount() == null) {
			rpt.setFavShopCount(0);
		}
		if (rpt.getIndirectCartTotal() == null) {
			rpt.setDirectCartTotal(0);
		}
		if (rpt.getIndirectPay() == null) {
			rpt.setIndirectPay(0);
		}
		if (rpt.getIndirectPayCount() == null) {
			rpt.setIndirectPayCount(0);
		}

		if (rpt.getIndirectCartTotal() == null) {
			rpt.setIndirectCartTotal(0);
		}
		if (rpt.getQscore() == null) {
			rpt.setQscore(0);
		}
		if (rpt.getWirelessQscore() == null) {
			rpt.setWirelessQscore(0);
		}
	}
}
